<?php
/**
 *
 * 用户控制器。
 *
 **/

namespace Home\Controller;

use Think\Controller;

class UserController extends Controller
{

    public function index(){

        $this->display();
    }
    //前台驾校约车记录列表----当选择科目三时
    public function ajax_index()
    {
        $user_id = session('user.id');

        $where['user_id'] = $user_id;

        $type = $_GET['type'];
        switch($type){
            case 'all': break;
            case '1':
            case '2':$where['course'] = $type;break;
        }
        $info = M('meeting_car_record ')
            ->join('as r  left join  driving_appointment_time  as  t   on  r.time_id = t.id  ')
            ->field('r.coach_name,r.coach_head,r.day,r.category,r.status,t.time as study_hours')
            ->where($where)
            ->order('r.id desc')
            ->select();

        foreach($info as $k => $v){
            $arr = explode('-',$v['study_hours']);
            $time = round((strtotime($arr[1]) - strtotime($arr[0])) / 3600 );
            $info[$k]['study_hours'] = $time;
        }

        $this->assign('record',$info);
        $this->display();
    }


    //前台驾校约车记录列表----当选择科目二时
    public  function  subject(){

        //获取登陆的用户信息1
        $UserId = session('user_id');
        $UserHead = session('user_head');
        //获取登陆的用户信息2
        $UserId = session('user_id');
        $UserId=1;
        $user = M('user');
        $data=$user->where('id ='.$UserId )->find();
        $head=$data['pro'];
//      dump($data);
//      exit;


        //搜索分页，加科目三
        $p= intval($_GET['p'])?$_GET['p']:1;
        $MeetingCarRecord = M('MeetingCarRecord');
        $pagesize = 2;#每页数量
        $offset = $pagesize * ($p - 1);//计算记录偏移量
        $prefix = C('DB_PREFIX');

        $where = 'mcr.user_id ='. $UserId.' and mcr.course = 1' ;

        $count= $MeetingCarRecord->join('as mcr left join  driving_appointment_time  as at    on mcr.time_id=at.id')
            ->field('mcr.id, mcr.user_id, mcr.user_name,  mcr.coach_head, mcr.coach_name, mcr.day, mcr.study_hours, mcr.category, mcr.course, mcr.status, at.time')
            ->where($where)
            ->order('mcr.id asc' )
            ->count();

        $data= $MeetingCarRecord->join('as mcr left join  driving_appointment_time  as at    on mcr.time_id=at.id')
            ->field('mcr.id, mcr.user_id, mcr.user_name, mcr.coach_name, mcr.coach_head, mcr.day, mcr.study_hours, mcr.category, mcr.course, mcr.status, at.time')
            ->where($where)
            ->order('mcr.id asc' )
            ->limit($offset . ',' . $pagesize)
            ->select();

//
//          dump($data);
//          exit;
//        dump($MeetingCarRecord->getLastSql());
//        exit;


        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();

        $this->assign('data', $data);
        $this->assign('page', $page);
        $this->display('index');


    }



}